<?php
namespace addons\ymyaws\library;

use Aws\Sns\SnsClient;
use think\Exception;

class Aws
{
    protected $key = null;
    protected $secret = null;
    protected $region = null;
    protected $error = null;
    public function __construct($config = [])
    {
        $addon_config = get_addon_config('ymyaws');
        $this->key = $config['key'] ?? $addon_config['key'];
        $this->secret = $config['secret'] ?? $addon_config['secret'];
        $this->region = $config['region'] ?? $addon_config['region'];
        $this->template = $addon_config['template'];
    }

    public function sms($mobile,$code,$event){
        $params = array(
            'credentials' => array(
                'key' => $this->key,
                'secret' => $this->secret,
            ),
            'region' => $this->region, // 服务器区域
            'version' => 'latest'
        );
        if(!isset($this->template[$event])){
            $this->error = '该短信模板不存在';
            return false;
        }
        try {
            $message = str_replace('{code}',$code,$this->template[$event]);
            $sns = new SnsClient($params);
            $args = array(
                "Message" => $message,
                "PhoneNumber" => $mobile
            );
            $result = $sns->publish($args);
            dump($result);exit;
        }catch (Exception $e){
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }
}